package com.funny.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.funny.springcloud.entities.Payment;
import com.funny.springcloud.utils.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author FUNNYWUS
 * @date 2020/7/29
 */
@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public R byResource() {
        return  new R(200,"按照资源名称限流测试",new Payment(2020L,"serial001"));
    }

    public R handleException(BlockException exception) {
        return  new R(444,exception.getClass().getCanonicalName() + "\t 服务不可用");
    }

    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl")
    public R byUrl() {
        return  new R(200,"按照byUrl限流测试",new Payment(2020L,"serial002"));
    }

    //CustomerBlockHandler
    @GetMapping("/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handlerException")
    public R customerBlockHandler() {
        return  new R(200,"按照客户自定义限流测试",new Payment(2020L,"serial003"));
    }

}


